#!/dis/sh
nbindexes = $home^/nbindexes
if {~ $1 '-x'} {(opt re res) = $*} {(re res) = $*}

tmp1 = /tmp/nb1.${pid}
tmp2 = /tmp/nb2.${pid}
grep $re `{cat $nbindexes} /dev/null | sed 's,nbindex: ,,' > $tmp1

apply {
	re=$1
	if {~ $re '-*'} {
		re = `{echo $re | sed 's,^-,,'}
		grep -v $re $tmp1 > $tmp2
	} {
		grep $re $tmp1 > $tmp2
	}
	mv $tmp2 $tmp1
} $res

cat $tmp1
rm -f $tmp1 $tmp2

if {! ~ $opt '-x'} {
	files=`{ls -d -F * | sed 's,\*$,,' | grep -v '.exe$' | grep -v '/$' | grep -v nbindex}
	grep -n '^#?nb ' $files /dev/null | grep -v '^nbindex' | sed 's,nb ,,' > nbindex

	echo `{pwd}^'/nbindex' >> $nbindexes
	sort $nbindexes | uniq > /tmp/nbindexes
	mv /tmp/nbindexes $nbindexes
}

exit
